{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "def multi_result_random(dist,itemSplitor,kvSplitor):\n",
    "    # dist like 'a:0.1^b:0.2^c:0.7', return a/b/c\n",
    "    choice_probs = dist.split(itemSplitor)\n",
    "    elevation = 0.0\n",
    "    choice_queue = []\n",
    "    total_volumn = 1e-6 + sum([float(choice_prob.split(kvSplitor)[1]) for choice_prob in choice_probs])\n",
    "    for choice_prob in choice_probs:\n",
    "        kv = choice_prob.split(kvSplitor)\n",
    "        if len(kv) < 2:\n",
    "            continue\n",
    "        choice,prob = kv[0],float(kv[1])/total_volumn\n",
    "        choice_queue.append((elevation + prob,choice))\n",
    "        elevation += prob\n",
    "    point = random.random()\n",
    "    for choice in choice_queue:\n",
    "        if choice[0] > point:\n",
    "            return choice[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{(3, 0): '0:0.8997^1:0.1003', (3, 1): '0:0.8622^1:0.1378', (2, 1): '0:0.8677^1:0.1323', (2, 0): '0:0.9078^1:0.0922', (5, 0): '0:0.8396^1:0.1604', (5, 1): '0:0.8223^1:0.1777', (1, 0): '0:0.9012^1:0.0988', (4, 1): '0:0.8464^1:0.1536', (1, 1): '0:0.8569^1:0.1431', (4, 0): '0:0.8791^1:0.1209'}\n",
      "1&0:321812^1&1:99520^2&0:780641^2&1:232648^3&0:1068848^3&1:322751^4&0:1093065^4&1:348678^5&0:642264^5&1:231744\n"
     ]
    }
   ],
   "source": [
    "prefer_file = open(\"prefer.csv\",\"r\")\n",
    "head = prefer_file.readline()\n",
    "\n",
    "preferDist = {}\n",
    "uvDist = []\n",
    "\n",
    "for line in prefer_file.readlines():\n",
    "    data = line.split(\",\")\n",
    "    if len(data)<4:\n",
    "        continue\n",
    "    level,gender = int(data[0]),int(data[1])\n",
    "    dist = data[2]\n",
    "    uv = int(data[3])\n",
    "    preferDist[(level,gender)] = dist\n",
    "    uvDist.append(str(level)+\"&\"+str(gender)+\":\"+str(uv))\n",
    "    \n",
    "print preferDist\n",
    "print '^'.join(uvDist)\n",
    "\n",
    "prefer_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "requestFile = open(\"request.csv\",\"w\")\n",
    "\n",
    "uvDistStr = '^'.join(uvDist)\n",
    "\n",
    "for t in range(1000):\n",
    "    people = multi_result_random(uvDistStr,\"^\",\":\")\n",
    "    level,gender = people.split(\"&\")\n",
    "    choice = multi_result_random(preferDist[int(level),int(gender)],\"^\",\":\")\n",
    "    #print t, level, gender, multi_result_random(preferDist[int(level),int(gender)],\"^\",\":\")\n",
    "    requestFile.write(str(t)+\",\"+level+\",\"+gender+\",\"+choice+\"\\n\")\n",
    "    \n",
    "requestFile.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
